import router from '@/router';
import axios from 'axios';
import { showToast } from 'vant';
import { baseURL } from '../plugins/config';
const axiosInstance = axios.create({
    baseURL: `${baseURL}/api`,
    timeout: 6000
});
// 请求拦截器
axiosInstance.interceptors.request.use(request => {
    const { token } = JSON.parse(localStorage.getItem('userLoginInfo') || '{}');
    if (token) {
        // 自动携带登陆凭证token
        request.headers.authorization = token;
    }
    return request;
});
// 响应拦截器
axiosInstance.interceptors.response.use(response => {
    if (response.data.code === 403) {
        showToast(response.data.msg);
        // 移除本地存储中的登陆信息
        localStorage.removeItem('userLoginInfo');
        // 登陆超时
        router.push({ name: "login",query:{url:router.currentRoute.value.fullPath} });
    }
    return response.data;
});


export const axiosPlugin = {
    install(app) {
        app.config.globalProperties.$axios = axiosInstance;
    }
}

export default axiosInstance;